package mx.netbrain.dao.impl;

import java.io.Serializable;
import java.util.List;

import mx.netbrain.dao.TreeDao;
import mx.netbrain.model.Status;
import mx.netbrain.model.Tree;
import mx.netbrain.model.User;
import mx.netbrain.util.HibernateUtil;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

public class TreeDaoImpl implements TreeDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1091954341581135958L;
	private static final Logger logger = Logger.getLogger(TreeDaoImpl.class);

	@Override
	public Tree findById(Long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Tree) session.load(Tree.class, id);
	}

	@Override
	public void save(Tree tree) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Status status = new Status();
		status.setId(1);
		tree.setStatus(status);
		try {
			session.beginTransaction();
			session.persist(tree);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Save: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public void update(Tree tree) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.merge(tree);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Update: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tree> listAllUser(User user) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select treeuser.tree from TreeUser as treeuser where "
				+ "treeuser.user.id = :user and treeuser.tree.status.id = 1 and "
				+ "treeuser.tree.root = 0";
		Query query = session.createQuery(sql);
		query.setLong("user", user.getId());
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tree> listNodeId(Long id, User user) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select tree from Tree as tree "
				+ "where tree.status.id = 1 and tree.root = :root and tree.type = :type";
		Query query = session.createQuery(sql);
		query.setLong("root", id);
		query.setString("type", "Carpeta");
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tree> listNodeFolder(Long id, User user) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select tree from Tree as tree where tree.root = :root and tree.status.id = 1";
		Query query = session.createQuery(sql);
		query.setLong("root", id);
		return query.list();
	}

	@Override
	public Tree findByRepository(Long repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select tree from Tree as tree where tree.repositories = :repo and tree.root = 0";
		Query query = session.createQuery(sql);
		query.setLong("repo", repo);
		return (Tree) query.uniqueResult();
	}

	@Override
	public boolean findTreeName(String name, Long repo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select tree from Tree as tree where tree.name = :name and tree.root = :repo";
		Query query = session.createQuery(sql);
		query.setString("name", name);
		query.setLong("repo", repo);
		if ((Tree) query.uniqueResult() != null) {
			return true;
		}
		return false;
	}

}
